package com.northpool.operator.common.json;

import cn.hutool.core.lang.Console;
import com.northpool.operator.annotation.InputParam;
import com.northpool.operator.annotation.InputParams;
import com.northpool.operator.annotation.OperatorInfo;
import com.northpool.operator.annotation.OutputParam;
import com.northpool.operator.annotation.OutputParams;
import com.northpool.operator.common.algorithm.OperatorTestBuild;
import com.northpool.operator.common.algorithm.TestParamBuilder;
import com.northpool.operator.param.ParamTool;
import com.northpool.operator.type.TypeEnum;
import com.northpool.operator.utils.StpOperatorHelper;
import org.apache.commons.lang3.StringEscapeUtils;

@OperatorInfo(
        ref = "JsonUnescapeOperator",
        name = "Json解码",
        description = "字符串中被转义的字符恢复为原始形式"
)
@OutputParams({
        @OutputParam(ref = "execResult", name = "转义结果", type = TypeEnum.STRING)
})
public class JsonUnescapeOperator extends StpOperatorHelper {

    @InputParams({
            @InputParam(ref = "str", name = "字符串", desc = "字符串,如: {\"name\":\"test\"}", required = true)
    })
    public JsonUnescapeOperator() {
    }

    public static void main(String[] args) {
        OperatorTestBuild.execute(JsonUnescapeOperator.class, TestParamBuilder.aBuilder()
                .param("str", "DLBM,tbmj\\r\\n\\t0102,359135.73\\r\\n\\t0201,4680.47\\r\\n\\r\\n\\t0102,359135.73\\r\\n\\t0201,4680.47\\r\\n")
                .build());
    }

    @Override
    public void execute(ParamTool paramTool) throws Exception {
        String str = paramTool.getStr("str");
        String result = StringEscapeUtils.unescapeJson(str);
        Console.log("解码前: {}",str);
        this.result("execResult", result);
    }

}
